package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.service.AdvimgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("ydd_advimg")
public class AdvimgController {
    @Autowired
    private AdvimgService advimgService;

    /**
     * 返回广告图详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("返回广告图详情id:{}", id);
        Advimg advimg = advimgService.getById(id);
        return ResultData.ok("", advimg);
    }

    /**
     * 广告图列表-分页
     *
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResultData list(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        log.info("广告图描述advimgdesc:{},advimgpos:{},页码page:{},每页数量limit:{}", advimgdesc, advimgpos, page, limit);
        HashMap<String, Object> map = advimgService.getPageHashMap(advimgdesc, advimgpos, page, limit);
        return ResultData.ok("", map);
    }

    /**
     * 广告图删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData del(Integer id) {
        log.info("广告图删除id:{}", id);
        advimgService.removeById(id);
        return ResultData.ok("", "广告图删除成功");
    }

    /**
     * 广告图修改
     *
     * @param advimg
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Advimg advimg) {
        log.info("分类修改Advimg:{}", advimg);
        advimgService.updateById(advimg);
        return ResultData.ok("", "广告图修改成功");
    }

    /**
     * 广告图添加
     *
     * @param advimg
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Advimg advimg) {
        log.info("广告图添加Cate:{}", advimg);
        advimg.setDefault_data("1");
        advimgService.save(advimg);
        return ResultData.ok("", "广告图添加成功");
    }

}
